package yxy.game.pm2.active.command.startup;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actstartupdata;
import yxy.cherry.data.bean.task;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_武将直升活动;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.GiftPackage;
import yxy.game.pm2.active.module.subordinateclass.ghost.GhostGachaTask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.bean.timeline.Timeline;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hero.HeroHandle;

import java.util.*;


/****************************************
 * @name CMD_Active_武将直升_商城和任务_Info
 * @description 武将直升商城和任务信息
 * @author 梁健彬
 * @since 2022-07-7
 ***************************************/
@MessageType(proto = Command.ActStartupShopAndTaskInfo, classtype = ActiveMessage.ActStartupShopAndTaskInfoRequest.class)
public class CMD_Active_武将直升_商城和任务_Info extends Command<ActiveMessage.ActStartupShopAndTaskInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActStartupShopAndTaskInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActStartupShopAndTaskInfoResponse.Builder response = ActiveMessage.ActStartupShopAndTaskInfoResponse.newBuilder();

        try {
            ActiveModule_武将直升活动 am = ActiveModule_武将直升活动.get(playercode, false);
            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            Map<String, actstartupdata> agrmap = ActDataCenter.getAllData(actstartupdata.class, playercode);

            //商城信息
            getGiftPackage(am, playercode, agrmap, response);

            //任务信息
            getGhostGachaTask(am, playercode, response);

            //开始结束时间
            Timeline timeline = am.getTimeline();
            long startTime = timeline.getStartTime().getMillis();
            long endTime = timeline.getEndTime().getMillis();

            //返回
            response.setStart(startTime);
            response.setEnd(endTime);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }


    /**
     * 任务信息
     */
    private void getGhostGachaTask(ActiveModule_武将直升活动 am, String playercode, ActiveMessage.ActStartupShopAndTaskInfoResponse.Builder response) {
        ArrayList<ActiveMessage.StartupTask> list = new ArrayList<>();

        for (GhostGachaTask gachaTask : am.getTaskMap().values()) {
            ActiveMessage.StartupTask.Builder build = ActiveMessage.StartupTask.newBuilder();

            build.setId(gachaTask.getId());//id
            build.setTaskId(gachaTask.getTaskid());//taskId
            build.setCode(gachaTask.getTaskcode());//taskcode
            build.setNeedStar(gachaTask.getNeedStar());//需要的星级
            Task taskbean = Task.get(playercode, gachaTask.getTaskcode());

            task data = DataCenter.getData(gachaTask.getTaskid(), task.class);
            Currencies curs = new Currencies(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));
            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
            build.addAllCurs(items);//奖励

            build.setProgress(taskbean.getProgress());
            int canget = taskbean.isReward() ? DrawStatus.hadget.getStatus() : taskbean.isFinish() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
            build.setCanget(canget);//是否可领取
            list.add(build.build());
        }

        //从低到高排序
        Collections.sort(list, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                ActiveMessage.StartupTask builder1 = (ActiveMessage.StartupTask) o1;
                ActiveMessage.StartupTask builder2 = (ActiveMessage.StartupTask) o2;
                return builder1.getNeedStar() - builder2.getNeedStar();
            }
        });

        response.addAllTaskList(list);

    }

    /**
     * 商城信息
     */
    private void getGiftPackage(ActiveModule_武将直升活动 am, String playercode, Map<String, actstartupdata> agrmap, ActiveMessage.ActStartupShopAndTaskInfoResponse.Builder response) {
        ArrayList<ActiveMessage.StartupShop> list = new ArrayList<>();

        //获取对应武将的最高星级
        int maxStar = HeroHandle.heroMaxStar(playercode, am.getHeroId());

        for (GiftPackage gp : am.getDgifts()) {
            ActiveMessage.StartupShop.Builder build = ActiveMessage.StartupShop.newBuilder();

            actstartupdata actstartupdata = agrmap.get(String.valueOf(gp.getId()));

            build.setId(actstartupdata.getId());//表id
            build.setGoodsid(actstartupdata.getChargeid());//商品id
            build.setLimitnum(actstartupdata.getLimit());//限购数量
            build.setBuynum(gp.getBuyNum());//购买的数量
            build.setNeedStar(gp.getNeedStar());//需要的星级
            Currencies curs = new Currencies(Currency.create(actstartupdata.getRewardtype(), actstartupdata.getRewardid(), actstartupdata.getRewardnum())); //奖励
            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
            build.addAllCurs(items);//商品物品

            //是否可购买：通过判断购买次数是否>=限购次数
            int canget = gp.getBuyNum() < gp.getNumlimit() && maxStar >= gp.getNeedStar() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
            build.setCanget(canget);//是否可购买
            list.add(build.build());
        }

        //从低到高排序
        Collections.sort(list, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                ActiveMessage.StartupShop builder1 = (ActiveMessage.StartupShop) o1;
                ActiveMessage.StartupShop builder2 = (ActiveMessage.StartupShop) o2;
                return builder1.getNeedStar() - builder2.getNeedStar();
            }
        });

        response.addAllShopList(list);
        response.setMaxStar(maxStar);
    }

    private void send(MessageSource messageSource, ActiveMessage.ActStartupShopAndTaskInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
